﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using UtilZ.Dotnet.Ex.Base;
using UtilZ.Dotnet.Ex.DataStatistic;
using UtilZ.Dotnet.Ex.Log;

namespace WinFormApp
{
    public partial class FTestDataStatistic : Form
    {
        public FTestDataStatistic()
        {
            InitializeComponent();
        }

        private ThreadEx _generatThread = null;
        private DataStatisticCallbackItem _statisticItem = null;
        private void FTestDataStatistic_Load(object sender, EventArgs e)
        {
            try
            {

                UtilZ.Dotnet.Ex.ZLog.ZLoger.OutputOption = UtilZ.Dotnet.Ex.ZLog.ZLogerOutputOptions.Loger;
                var redirectAppenderToUI = (RedirectAppender)Loger.GetAppenderByName(null, "RedirectToUI");
                if (redirectAppenderToUI != null)
                {
                    redirectAppenderToUI.RedirectOuput += RedirectLogOutput;
                }
                else
                {
                    this.logControl.AddLog(string.Format("{0} {1}", DateTime.Now.ToString(LogConstant.DateTimeFormat), "日志配置文件不存在"), LogLevel.Warn);
                }

                this._generatThread = new ThreadEx(this.GeneratThreadMethod, "xxx");
                this._statisticItem = new DataStatisticCallbackItem(2000, StatisticOutputCallback);                
            }
            catch (Exception ex)
            {
                Loger.Error(ex);
            }
        }

        private void RedirectLogOutput(object sender, RedirectOuputArgs e)
        {
            try
            {
                if (this.IsDisposed)
                {
                    return;
                }

                this.logControl.AddLog(string.Format("{0} {1}", e.Item.Time.ToString(LogConstant.DateTimeFormat), e.Item.Content), e.Item.Level);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
        private void btnClear_Click(object sender, EventArgs e)
        {
            this.logControl.Clear();
        }


        private long _value = 0L;
        private void StatisticOutputCallback(DataStatisticOutputArgs args)
        {
            var value = Interlocked.Exchange(ref this._value, 0L);
            Loger.Info($"{args.Duration}ms内,value:{value}");
        }

        private void GeneratThreadMethod(ThreadPara threadPara)
        {
            Loger.Info("开始生成数据");
            while (!threadPara.Token.IsCancellationRequested)
            {
                Interlocked.Increment(ref this._value);
                threadPara.WaitOne(500);
            }
            Loger.Info("停止生成数据");
        }

        private void btnThread_Click(object sender, EventArgs e)
        {
            if (this._generatThread.IsRuning)
            {
                this._generatThread.Stop();
            }
            else
            {
                this._generatThread.Start();
            }
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            DataStatisticManager.Default.Start();
        }

        private void btnStop_Click(object sender, EventArgs e)
        {
            DataStatisticManager.Default.Stop();
        }

        private void btnAdd_Click(object sender, EventArgs e)
        {
            DataStatisticManager.Default.Add(this._statisticItem);
        }

        private void btnRemove_Click(object sender, EventArgs e)
        {
            DataStatisticManager.Default.Remove(this._statisticItem);
        }
    }
}
